package com.edu.service.impl;

import com.edu.dao.BookDao;
import com.edu.dao.CategoriesDao;
import com.edu.dao.impl.BookDaoImpl;
import com.edu.dao.impl.CategoriesDaoImpl;
import com.edu.entity.BookEntity;
import com.edu.service.BookService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * com.edu.service.impl
 * 2024/7/4 21:00
 *
 * @author Fancy
 * @version 1.0
 */
public class BookServiceImpl implements BookService {
    BookDao bookDao = new BookDaoImpl();
    CategoriesDao categoriesDao = new CategoriesDaoImpl();

    @Override
    public List<HashMap<String, Object>> getAllBook() {
        List<HashMap<String, Object>> book = bookDao.getAllBook();
        return book;
    }

    @Override
    public Integer deleteBookInfo(String bookId) {
        return bookDao.deleteBookInfo(bookId);
    }

    @Override
    public HashMap<String, Object> getBookInfoById(String bookId) {
        return bookDao.getBookInfoById(bookId);
    }

    @Override
    public Integer updateBookInfo(BookEntity book) {
        return bookDao.updateBookInfo(book);
    }

    @Override
    public Integer addBookInfo(BookEntity book) {
        return bookDao.addBookInfo(book);
    }

    @Override
    public HashMap<String, Object> getBookInfoBySearch(String search) {
        //查找符合条件的bookId
        ArrayList<Integer> bookIds = bookDao.getBookIdInfoBySearch(search);
        System.out.println(bookIds.toString());
        //根据bookId查找对应的书籍
        LinkedList<BookEntity> booksInfo = new LinkedList<>();
        for (int bookId :
                bookIds) {
            booksInfo.add(bookDao.getBookInfoById(bookId));
        }
        HashMap<String, Object> result = new HashMap<>();
        result.put("totalItems",bookIds.toArray().length);
        result.put("books",booksInfo);
        return result;
    }
}
